package com.gxj.study.shopdemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gxj.study.shopdemo.http.BaseCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 祥杰 on 2016/8/6.
 */

public abstract class BaseAdapter<T, H extends BaseViewHolder> extends RecyclerView.Adapter<BaseViewHolder> {
    protected static final String TAG = BaseAdapter.class.getSimpleName();
    protected final Context context;
    protected final int layoutResId;
    protected final List<T> datas;
    private OnItemClickListener mOnItemClickListener = null;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public BaseAdapter(int layoutResId, Context context) {
        this(context, layoutResId, null);
    }

    public BaseAdapter(Context context, int layoutResId, List<T> datas) {
        this.context = context;
        this.layoutResId = layoutResId;
        this.datas = datas == null ? new ArrayList<T>() : datas;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(layoutResId, parent, false);
        BaseViewHolder viewHolder = new BaseViewHolder(view, mOnItemClickListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        T item = getItem(position);
        convert((H) holder, item);
    }

    @Override
    public int getItemCount() {
        if (datas == null || datas.size() <= 0) {
            return 0;
        }
        return datas.size();
    }

    public T getItem(int position) {
        if (position > datas.size()) return null;
        return datas.get(position);
    }

    public void clearData() {
        int itemCount = datas.size();
        datas.clear();
        this.notifyItemRangeChanged(0, itemCount);
    }

    public List<T> getDatas() {
        return datas;
    }

    public void addData(List<T> datas) {
        addData(0, datas);
    }

    public void addData(int position, List<T> datas) {
        if (datas != null && datas.size() > 0) {
            this.datas.addAll(datas);
            this.notifyItemRangeChanged(position, datas.size());
        }
    }

    public void refreshData(List<T> list) {

        clearData();
        if (list != null && list.size() > 0) {


            int size = list.size();
            for (int i = 0; i < size; i++) {
                datas.add(i, list.get(i));
                notifyItemInserted(i);
            }

        }
    }

    public void loadMoreData(List<T> list) {

        if (list != null && list.size() > 0) {

            int size = list.size();
            int begin = datas.size();
            for (int i = 0; i < size; i++) {
                datas.add(list.get(i));
                notifyItemInserted(i + begin);
            }

        }

    }

    protected abstract void convert(H viewHolder, T item);

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

}
